export class PlotUtils {
    /**
     * 计算两点之间的距离
     */
    static distance(p1, p2) {
        return Math.sqrt(Math.pow(p1[0] - p2[0], 2) + Math.pow(p1[1] - p2[1], 2));
    }

    /**
     * 获取中间点
     */
    static mid(p1, p2) {
        return [(p1[0] + p2[0]) / 2, (p1[1] + p2[1]) / 2];
    }

    /**
     * 计算控制点
     */
    static getControlPoints(points) {
        if (points.length < 3) return points;
        
        let controlPoints = [];
        for (let i = 0; i < points.length - 2; i++) {
            const p1 = points[i];
            const p2 = points[i + 1];
            const p3 = points[i + 2];
            
            const mid1 = this.mid(p1, p2);
            const mid2 = this.mid(p2, p3);
            
            controlPoints.push(mid1);
            controlPoints.push([p2[0], p2[1]]);
            controlPoints.push(mid2);
        }
        return controlPoints;
    }

    /**
     * 计算贝塞尔曲线点
     */
    static getBezierPoints(points, precision = 100) {
        if (points.length < 2) return points;
        
        const controlPoints = this.getControlPoints(points);
        let bezierPoints = [];
        
        for (let i = 0; i < controlPoints.length - 2; i += 3) {
            const p1 = controlPoints[i];
            const p2 = controlPoints[i + 1];
            const p3 = controlPoints[i + 2];
            
            for (let t = 0; t <= precision; t++) {
                const percent = t / precision;
                bezierPoints.push(this.getBezierPoint(p1, p2, p3, percent));
            }
        }
        return bezierPoints;
    }

    /**
     * 计算单个贝塞尔曲线点
     */
    static getBezierPoint(p1, p2, p3, percent) {
        const x = Math.pow(1 - percent, 2) * p1[0] + 
                 2 * percent * (1 - percent) * p2[0] + 
                 Math.pow(percent, 2) * p3[0];
        const y = Math.pow(1 - percent, 2) * p1[1] + 
                 2 * percent * (1 - percent) * p2[1] + 
                 Math.pow(percent, 2) * p3[1];
        return [x, y];
    }
} 